package com.daqsoft.controller.process;

import com.daqsoft.data.User;
import com.daqsoft.data.project.Project;
import com.daqsoft.data.project.Task;
import com.daqsoft.dto.GeneralResultEnum;
import com.daqsoft.permission.Verify;
import com.daqsoft.permission.VerifyType;
import com.daqsoft.service.process.ProjectFileProcessService;
import com.daqsoft.service.project.ProjectService;
import com.daqsoft.service.project.TaskService;
import com.daqsoft.service.system.UserManageService;
import com.daqsoft.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 项目文件管理流程
 * 包含连个流程 : 项目文件新增和项目文件删除
 *  1. 项目文件新增 -> 提交任务 -> 上传文件 -> 审核 -> 审核通过
 *  2. 项目文件删除 -> 提交任务 -> 选择要删除的文件 -> 审核 -> 审核通过
 * @author 周麟东 on 16:17
 */
@RestController
@RequestMapping("projectFileProcess")
public class ProjectFileProcessController {

    @Autowired
    private ProjectService projectService;
    @Autowired
    private UserManageService userService;
    @Autowired
    private ProjectFileProcessService service;
    @Autowired
    private TaskService taskService;

    @RequestMapping("submitAdd")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "提交项目新增文件请求")
    public GeneralResultEnum submitAdd(HttpServletRequest request , int projectId , int verifierId , String desc , Integer id){
        User principal  = (User) request.getSession().getAttribute("user");
        Project project = projectService.get(projectId);
        if (project == null){
            return GeneralResultEnum.FAIL.setDesc("项目不存在 , id = "+projectId);
        }
        User verifier = userService.findOne(verifierId);
        if (verifier == null){
            return GeneralResultEnum.FAIL.setDesc("审核人不存在 , id = "+ verifierId);
        }
        Task res = service.submitAdd(verifier , principal , project , desc , id);
        if (res != null){
            return GeneralResultEnum.SUCCESS.setData(res.toMap()).setDesc("提交成功!");
        }
        return GeneralResultEnum.FAIL.setDesc("提交失败 , 该项目已经存在文件变更任务");
    }
    @RequestMapping("submitDelete")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "提交项目删除文件请求")
    public GeneralResultEnum submitDelete(HttpServletRequest request , @RequestParam("ids") int [] deleteIds , int projectId , int verifierId , String desc , Integer id){
        User principal = (User) request.getSession().getAttribute("user");
        Project project = projectService.get(projectId);
        if (project == null){
            return GeneralResultEnum.FAIL.setDesc("项目不存在 , id = "+projectId);
        }
        User verifier = userService.findOne(verifierId);
        if (verifier == null){
            return GeneralResultEnum.FAIL.setDesc("审核人不存在 , id = "+ verifierId);
        }
        Task res = service.submitDelete(verifier , principal , project , desc , id , deleteIds);
        if (res != null){
            return GeneralResultEnum.SUCCESS.setData(res.toMap()).setDesc("提交成功!");
        }
        return GeneralResultEnum.FAIL.setDesc("提交失败 , 该项目已经存在文件变更任务");
    }

    @RequestMapping("checkAdd")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "审核项目新增文件请")
    public GeneralResultEnum checkAdd(HttpServletRequest request , String status , String msg , int id){
        User user = (User) request.getSession().getAttribute("user");
        Task task = taskService.get(id);
        if (task == null){
            return GeneralResultEnum.FAIL.setDesc("任务ID无法找到 , id = "+id);
        }
        String m = Log.add(user,task.getMsg(),"审核文件 -> ["+status+"] :"+msg);
        task.setMsg(m);
        service.checkAdd(task,status);
        return GeneralResultEnum.SUCCESS.setDesc("审核完成");
    }
    @RequestMapping("checkDelete")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "审核项目新增文件请")
    public GeneralResultEnum checkDelete(HttpServletRequest request , String status , String msg , int id){
        User user = (User) request.getSession().getAttribute("user");
        Task task = taskService.get(id);
        if (task == null){
            return GeneralResultEnum.FAIL.setDesc("任务ID无法找到 , id = "+id);
        }
        String m = Log.add(user,task.getMsg(),"审核文件 -> ["+status+"] :"+msg);
        task.setMsg(m);
        service.checkDelete(task,status);
        return GeneralResultEnum.SUCCESS.setDesc("审核完成");
    }

}
